package com.example.demo.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


import com.example.demo.bean.CmsArticle;
import com.example.demo.common.Response;
import com.example.demo.service.CmsArticleService;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

@RestController
@Api(tags = "文章管理服务")
@RequestMapping("/article")
public class CmsArticleController {

	@Autowired
	CmsArticleService articleService;
	

	@ApiOperation(value = "添加文章信息")

	@PostMapping("/insert")
	public Response<String> insertData(CmsArticle article){
		articleService.insert(article);
		return Response.ok("success");
	}
	
	@ApiOperation(value = "按照id删除")
	@ApiImplicitParam(name="id",value="学号",dataType="string",required=true)
	@DeleteMapping("/deleteById")
	public Response<String> deleteByIds(String id){
		int count = articleService.deleteById(id);
		return Response.ok("删除成功"+count+"条栏目!");
	}
	
	@ApiOperation(value = "保存或者更新文章信息",notes = "如果给定文章编号，那么代表更新文章信息<br/>"+"如果没有给定文章编号，那么代表新增文章信息")
	@PostMapping("/saveOrUpdate")
	public Response<String> saveOrUpdate(CmsArticle article){
		articleService.saveOrUpdate(article);
		return Response.ok("操作成功!");
	}
	
	@ApiOperation(value = "列出表中所有数据")
	@GetMapping("/All")
	public Response<List<CmsArticle>> findAll(){
		List<CmsArticle> all = articleService.findAll();
		return Response.ok(all);
	}
	
	@ApiOperation(value = "按id来找出表中数据")
	@GetMapping("/select")
	public Response<CmsArticle> selectById(String id){
		CmsArticle data = articleService.selectById(id);
		return Response.ok(data);
	}

}

